package Impor;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import dao.BaseDao;
import dao.StuDao;
import entity.ExamResult;
import entity.Student;

public class StuDaoMySqlImpl extends BaseDao implements StuDao {

	@Override
	public int add(Student s) {
		// 添加
		String sql = "insert into student (stuno,sname,gradeid ) values (?,?,?)";
		Object[] add = { s.getStuno(), s.getSname(), s.getGradeid() };
		int row = super.exeUpdate(sql, add);
		return row;
	}

	@Override
	public int update(Student s) {
		// 更新修改学生
		String sql = "update student set gradeid =? where stuno =?";
		Object[] upda = { s.getGradeid(), s.getStuno() };
		int row = super.exeUpdate(sql, upda);
		return row;
	}

	@Override
	public int del(Student s) {
		// 删除
		String sql = "delete from student where stuno = ?";
		Object[] del = { s.getStuno() };
		int row = super.exeUpdate(sql, del);
		return row;
	}

	@Override
	// 查询指定学号的学生信息
	public Student get(int id) {
		String sql = "select * from student where stuno=?";
		Student s = null;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = super.getConnection();
			ps = con.prepareStatement(sql);
			ps.setInt(1, id); // 方法参数指定要查询的学生信息
			rs = ps.executeQuery();
			if (rs.next()) {
				s = new Student();
				s.setStuno(id);
				s.setSname(rs.getString("sname"));// 查询结果集做参数，来修改学生类里面的数据
				s.setAddress(rs.getString("address"));
				s.setGradeid(rs.getInt("gradeid"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeAll(con, ps, rs);
		}
		return s;
	}

	// 添加学生考试分数记录
	public int addScore(int id, float score, int st_no, int sb_no) {
		String sql = "INSERT INTO examresult(id,score,st_id,sb_id) VALUES (?,?,?,?) ";
		Object[] arg = { id, score, st_no, sb_no };
		int row = super.exeUpdate(sql, arg);
		return row;
	}

	@Override
	//登陆验证
	public boolean login(String stuno, String pass) {
		String sql = "select * from student where stuno=? and password=?";
		int no = (int)Integer.parseInt(stuno);
		Student stu = new Student(no,pass);
		boolean bool = super.Login(sql, stu);
		if(bool==true) {
			return true;
		}else {
			return false;
		}	
	}

		// 按学号查看学生成绩
		public ExamResult findScore(String stuno) {
			String sql = "select id ,score,st_id,sb_id,exam_date from examresult where st_id=" + stuno ;
			Connection con = super.getConnection();
			PreparedStatement ps = null;
			ResultSet rs = null;
			ExamResult er = null;
			try {
				ps = con.prepareStatement(sql);
				rs = ps.executeQuery();
				while (rs.next()) {
					er = new ExamResult();
					er.setId(rs.getInt("id"));
					er.setExam_date(rs.getString("exam_date"));
					er.setScore(rs.getDouble("score"));
					er.setSt_id(rs.getInt("st_id"));
					er.setSb_id(rs.getInt("sb_id"));
				}
				
			} catch (Exception e) {
				e.printStackTrace();

			} finally {
				super.closeAll(con, ps, rs);
			}
			
			return er;
		}

}
